Project (PRODATA)
cmake_minimum_required(VERSION 3.2)

if(NOT WIN32)
  string(ASCII 27 Esc)
  set(ColourReset "${Esc}[m")
  set(ColourBold  "${Esc}[1m")
  set(Red         "${Esc}[31m")
  set(Green       "${Esc}[32m")
  set(Yellow      "${Esc}[33m")
  set(Blue        "${Esc}[34m")
  set(Magenta     "${Esc}[35m")
  set(Cyan        "${Esc}[36m")
  set(White       "${Esc}[37m")
  set(BoldRed     "${Esc}[1;31m")
  set(BoldGreen   "${Esc}[1;32m")
  set(BoldYellow  "${Esc}[1;33m")
  set(BoldBlue    "${Esc}[1;34m")
  set(BoldMagenta "${Esc}[1;35m")
  set(BoldCyan    "${Esc}[1;36m")
  set(BoldWhite   "${Esc}[1;37m")
endif()

macro(AddDef def msg)
    set(compile_defs ${compile_defs} ${def})
    message("${Magenta}++ Macro ${def}: ${msg}${ColourReset}")
endmacro()

Option(CMAKE_VERBOSE_MAKEFILE "verbose make file" OFF)
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

message(STATUS "${Green}PRODATA_BINARY_DIR = ${PRODATA_BINARY_DIR}${ColourReset}")
message(STATUS "${Green}PRODATA_SOURCE_DIR = ${PRODATA_SOURCE_DIR}${ColourReset}")

Option(PETSC "Build  with petsc" ON)
Option(PETSC_DEFINITION "Build with definition -DPETSC" ON)
Option(PETSC_DEBUG "Build with definition -DPETSC_DEBUG" OFF)
set(PETSC_WITH_EXTERNAL_LIB "" CACHE STRING "PETSc lib linkers")
set(PETSC_INCLUDES "" CACHE STRING "PETSc lib includes")

if(PETSC)
    set(PETSC OFF)
    set(PETSC_DEFINITION OFF)
    if(DEFINED ENV{PETSC_DIR})
        set(PETSC ON)
        set(PETSC_DEFINITION ON)
        
        file(STRINGS "$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/petsc/conf/petscvariables" PETSC_EXTERNAL_LIB REGEX "PETSC_EXTERNAL_LIB_BASIC = .*$?")
        string(REPLACE "PETSC_EXTERNAL_LIB_BASIC = " "" PETSC_EXTERNAL_LIB "${PETSC_EXTERNAL_LIB}")
        set(PETSC_WITH_EXTERNAL_LIB "-L$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib -lpetsc ${PETSC_EXTERNAL_LIB}")
        message(STATUS "${Green}PETSC_WITH_EXTERNAL_LIB = ${PETSC_WITH_EXTERNAL_LIB}${ColourReset}")


        file(STRINGS "$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/petsc/conf/petscvariables" PETSC_INCLUDES REGEX "PETSC_CC_INCLUDES = -I.*$?")
        string(REPLACE "PETSC_CC_INCLUDES = -I" "" PETSC_INCLUDES "${PETSC_INCLUDES}")
        message(STATUS "${Green}PETSC_INCLUDES = ${PETSC_INCLUDES}${ColourReset}")

      # file(STRINGS "$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/petsc/conf/petscvariables" CMAKE_CXX_COMPILER REGEX "CXX_LINKER = .*$?")
      # string(REPLACE "CXX_LINKER = " "" CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER}")
      # message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")

        file(STRINGS "$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/petsc/conf/petscvariables" PETSC_CXX_LINKER_FLAGS REGEX "CXX_LINKER_FLAGS = .*$?")
        string(REPLACE "CXX_LINKER_FLAGS = " "" PETSC_CXX_LINKER_FLAGS "${PETSC_CXX_LINKER_FLAGS}")
        message(STATUS "${Green}PETSC_CXX_LINKER_FLAGS = ${PETSC_CXX_LINKER_FLAGS}${ColourReset}")
        
        set(PETSC_CXX_LINKER_FLAGS "${PETSC_CXX_LINKER_FLAGS}")

        file(STRINGS "$ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib/petsc/conf/petscvariables" PETSC_CONFIGURE_OPTIONS REGEX "CONFIGURE_OPTIONS = .*$?")
        string(REPLACE "CONFIGURE_OPTIONS = " "" PETSC_CONFIGURE_OPTIONS "${PETSC_CONFIGURE_OPTIONS}")
        message(STATUS "${Green}PETSC_CONFIGURE_OPTIONS = ${PETSC_CONFIGURE_OPTIONS}${ColourReset}")


	else()
        set(PETSC_DEFINITION OFF)
        set(PETSC_DEBUG OFF)
		set(ESH3D OFF)
    endif()
else()
    set(PETSC_DEFINITION OFF)
    set(PETSC_DEBUG OFF)
endif()

execute_process(COMMAND gsl-config --cflags TIMEOUT 5 OUTPUT_VARIABLE GSL_INCLUDE OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REGEX MATCHALL "-I[a-zA-Z0-9/\._-]*" GSL_INCLUDE "${GSL_INCLUDE}")
string(REPLACE "-I" "" GSL_INCLUDE "${GSL_INCLUDE}")
message(STATUS "${Green} GSL_INCLUDE = ${GSL_INCLUDE}${ColourReset}")
execute_process(COMMAND gsl-config --libs TIMEOUT 5 OUTPUT_VARIABLE GSL_LINK_FLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "${Green} GSL_LINK_FLAGS = ${GSL_LINK_FLAGS}${ColourReset}")

#find_package(GSL REQUIRED)
#if(GSL_FOUND)
#	message(STATUS "${Green} GSL_CBLAS_LIBRARY = ${GSL_CBLAS_LIBRARY}${ColourReset}")
#	message(STATUS "${Green} GSL_CBLAS_LIBRARY_DEBUG = ${GSL_CBLAS_LIBRARY_DEBUG}${ColourReset}")
#	message(STATUS "${Green} GSL_CONFIG_EXECUTABLE = ${GSL_CONFIG_EXECUTABLE}${ColourReset}")
#	message(STATUS "${Green} GSL_LIBRARY_DEBUG = ${GSL_LIBRARY_DEBUG}${ColourReset}")
#endif()

find_package(MPI REQUIRED)
if(MPI_FOUND)  
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MPI_EXE_LINKER_FLAGS}")  
endif()  

Set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" "GDB")
if(NOT CMAKE_BUILD_TYPE)
    Set(CMAKE_BUILD_TYPE "RelWithDebInfo")
    message(STATUS "${Green}Default CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}${ColourReset}")
endif(NOT CMAKE_BUILD_TYPE)

Option(INSTALL_IN_PRODATA_SOURCE_DIR "install executable files in ${PRODATA_SOURCE_DIR}" ON)
if(INSTALL_IN_PRODATA_SOURCE_DIR)
    Set(CMAKE_INSTALL_PREFIX ${PRODATA_SOURCE_DIR} CACHE PATH "Install directory" FORCE)
endif()
message(STATUS "${Green}CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}${ColourReset}")

set(PRODATA_EXE prodata CACHE STRING "ProData executable file")

Option(OPENMP_MODE "Build paradis using definition OPENMP_MODE" ON)
if(OPENMP_MODE)
    find_package(OpenMP)  
    if(OPENMP_FOUND)  
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")  
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")  
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")  
        message(STATUS "${Yellow}OpenMP_C_FLAGS = ${OpenMP_C_FLAGS}${ColourReset}")
        message(STATUS "${Yellow}OpenMP_CXX_FLAGS = ${OpenMP_CXX_FLAGS}${ColourReset}")
        message(STATUS "${Yellow}OpenMP_EXE_LINKER_FLAGS = ${OpenMP_EXE_LINKER_FLAGS}${ColourReset}")
    else(OPENMP_FOUND)
        Option(OPENMP_MODE OFF)
    endif(OPENMP_FOUND)  
endif(OPENMP_MODE)


if(0)
Find_Package(GEOS)
Message(STATUS "include and lib " ${GEOS_INCLUDE_DIR} " " ${GEOS_LIB_DIR})
IF(GEOS_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GEOS_C_FLAGS}")  
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GEOS_CXX_FLAGS}")  
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GEOS_EXE_LINKER_FLAGS}")  
    message(STATUS "${Yellow}GEOS_INCLUDE_DIR = ${GEOS_INCLUDE_DIR}${ColourReset}")
    message(STATUS "${Yellow}GEOS_LIB_DIR = ${GEOS_LIB_DIR}${ColourReset}")
    message(STATUS "${Yellow}GEOS_C_FLAGS = ${GEOS_C_FLAGS}${ColourReset}")  
    message(STATUS "${Yellow}GEOS_CXX_FLAGS = ${GEOS_CXX_FLAGS}${ColourReset}")  
    message(STATUS "${Yellow}GEOS_EXE_LINKER_FLAGS = ${GEOS_EXE_LINKER_FLAGS}${ColourReset}")  
    Message(STATUS "${Yellow}GEOS_INCLUDE_DIR = ${GEOS_INCLUDE_DIR}${ColourReset}")
    Message(STATUS "${Yellow}GEOS_LINK_DIR = ${GEOS_LINK_DIR}${ColourReset}")
ENDIF(GEOS_FOUND)
endif()


set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unknown-pragmas -fopenmp" )

if(PETSC_DEFINITION)
    AddDef(-DPETSC "Introduce PETSc library")
    if(PETSC_DEBUG)
        AddDef(-DPETSC_DEBUG "Debug in PETSc functions")
    endif()
endif()

set(CMAKE_CXX_FLAGS "${PETSC_CXX_LINKER_FLAGS} ${CMAKE_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${PETSC_CXX_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
if(CMAKE_BUILD_TYPE STREQUAL "GDB")
    Set(CMAKE_C_FLAGS_GDB "-ggdb" )
    Set(CMAKE_CXX_FLAGS_GDB "-ggdb")
	Set(CMAKE_Fortran_FLAGS_GDB "-ggdb")

    AddDef(-DMPI_DEBUG "GDB build type, debug with MPI")

    Set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_GDB}")
    Set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_GDB}")
	Set(CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS} ${CMAKE_Fortran_FLAGS_GDB}")

    string(REPLACE "-O3" "" CMAKE_C_FLAGS  "${CMAKE_C_FLAGS}")
    string(REPLACE "-O3" "" CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS}")
    string(REPLACE "-O3" "" CMAKE_Fortran_FLAGS  "${CMAKE_Fortran_FLAGS}")
    string(REPLACE "-O3" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
endif()

Add_subdirectory (src)
